

package cn.shengchao.controler;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.shengchao.examstar.distribute.infrastrure.po.DistributeNodeInfo;
import cn.shengchao.examstar.distribute.infrastrure.service.DistributeNodeInfoService;
import cn.shengchao.examstar.rpc.distribute.enums.DistributeSaleRoleEnum;
import cn.shengchao.util.helper.LoginHelper;
import cn.shengchao.examstar.share.ohs.local.appservice.ShareAppService;
import cn.shengchao.examstar.share.ohs.local.message.ShareLinkDto;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.continew.starter.web.model.R;

import java.util.List;

@Tag(name = "分享")
@Slf4j
@RestController
@CrossOrigin("${project.cross-origin}")
@RequestMapping("/api/${project.version}/share")
public class ShareController {

    @Autowired
    private ShareAppService shareAppService;

    @Autowired
    private DistributeNodeInfoService distributeNodeInfoService;

    @GetMapping("canShare")
    public R canShare() {
        Long userId = LoginHelper.getUserId();
        DistributeNodeInfo distributeNodeInfo = distributeNodeInfoService.getDistributeNodeInfoInCode(userId, List
                .of(DistributeSaleRoleEnum.ROLE_DSPD_EMP, DistributeSaleRoleEnum.ROLE_DSPD, DistributeSaleRoleEnum.ROLE_SLMB));
        if (distributeNodeInfo != null) {
            return R.ok(Maps.immutableEntry("canShare", true));
        }
        return R.ok(Maps.immutableEntry("canShare", false));
    }

    @GetMapping("{shareCode}/qrCode")
    public R getShareQrCode(@PathVariable String shareCode,@RequestParam String path,@RequestParam String type) throws Exception {
        Long userId = LoginHelper.getUserId();
        log.info("获取分享二维码,userId:{},shareCode:{}", userId, shareCode);
        return R.ok(Maps.immutableEntry("qrCode", shareAppService.getShareQrCode(userId, path, type,shareCode, LoginHelper.getNickname(userId))));
    }

    @GetMapping
    public R createShareLink(ShareLinkDto shareLinkDto) throws Exception {
        Long userId = LoginHelper.getUserId();
        shareLinkDto.setUserId(userId);
        String nickName = LoginHelper.getNickname(userId);
        shareLinkDto.setNickName(nickName);
        log.info("创建分享链接，shareLinkDto:{}", shareLinkDto);
        return R.ok(Maps.immutableEntry("shareCode", shareAppService.createShareLink(shareLinkDto)));
    }

    @GetMapping("{shareCode}")
    public R clickShareLink(@PathVariable String shareCode) throws Exception {
        Long userId = LoginHelper.getUserId();
        log.info("点击分享链接,userId:{},shareCode:{}", userId, shareCode);
        shareAppService.clickShareLink(userId, shareCode);
        return R.ok();
    }

    @SaIgnore
    @GetMapping("{shareCode}/info")
    public R getShareInfo(@PathVariable String shareCode) throws Exception {
        log.info("获取分享信息,shareCode:{}", shareCode);
        return R.ok(shareAppService.getShareInfo(shareCode).orElseGet(() -> null));
    }

}
